package conf2;

import conf2.model.GameType;
import conf2.model.ShelobRule;
import conf2.model.SpecialCardRule;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Logger;

public class Configuration {

    public static final String DEFAULT_FILE = "Confrontation.ini";

    public static class CorruptedException extends Exception {
    }
    private Properties prop;

    private static Properties createDefault() {
        Properties p = new Properties();
        p.setProperty("ip", "127.0.0.1");
        p.setProperty("port", "11234");
        p.setProperty("sound", "false");
        p.setProperty("popup", "false");
        p.setProperty("specialcardreminder", "true");
        p.setProperty("movementassist", "true");
        p.setProperty("playername", "Player");
        p.setProperty("shelobrule", ShelobRule.SHELOB_DIES.toString());
        p.setProperty("gametype", GameType.CLASSIC.toString());
        p.setProperty("specialcardrule", SpecialCardRule.CLASSIC.toString());
        return p;
    }

    public Configuration() {
        prop = createDefault();
    }

    public static String getProperFilePath() {
        String os = System.getProperty("os.name");
        String path = DEFAULT_FILE;
        if (os == null) {
            // use default value
        } else if (os.startsWith("Windows")) {
            path = System.getenv("APPDATA");
            if (!path.endsWith("\\")) {
                path += "\\";
            }
            path += DEFAULT_FILE;
        } else if (os.startsWith("Linux") || os.startsWith("Mac OS X") ||
                os.startsWith("SunOS") || os.startsWith("Solaris")) {
            path = System.getProperty("user.home");
            if (!path.endsWith("/")) {
                path += "/";
            }
            path += DEFAULT_FILE;
        }        
        return path;
    }

    public void load(String fileName) throws IOException, CorruptedException {
        Properties p = new Properties();
        p.load(new FileReader(fileName));

        String[] toCheck = new String[]{"ip", "port", "sound", "popup", "specialcardreminder",
            "movementassist", "playername", "shelobrule", "gametype", "specialcardrule"};

        for (String key : toCheck) {
            if (p.getProperty(key) == null) {
                throw new CorruptedException();
            }
        }

        prop = p;
    }

    public void save(String fileName) throws IOException {
        if (fileName == null) {
            throw new NullPointerException("No filename has been given.");
        }
        FileOutputStream fos = new FileOutputStream(fileName);
        prop.store(fos, "Confrontation 2 configuration");
        fos.close();
    }

    public synchronized String getIP() {
        return prop.getProperty("ip");
    }

    public synchronized void setIP(String ip) {
        prop.setProperty("ip", ip);
    }

    public synchronized String getPort() {
        return prop.getProperty("port");
    }

    public synchronized void setPort(int port) {
        prop.setProperty("port", String.valueOf(port));
    }

    public synchronized boolean getSound() {
        return Boolean.parseBoolean(prop.getProperty("sound"));
    }

    public synchronized void setSound(boolean on) {
        prop.setProperty("sound", String.valueOf(on));
    }

    public synchronized boolean getPopup() {
        return Boolean.parseBoolean(prop.getProperty("popup"));
    }

    public synchronized void setPopup(boolean on) {
        prop.setProperty("popup", String.valueOf(on));
    }

    public boolean getSpecialCardReminder() {
        return Boolean.parseBoolean(prop.getProperty("specialcardreminder"));
    }

    public synchronized void setSpecialCardReminder(boolean on) {
        prop.setProperty("specialcardreminder", String.valueOf(on));
    }

    public synchronized boolean getMovementAssist() {
        return Boolean.parseBoolean(prop.getProperty("movementassist"));
    }

    public synchronized void setMovementAssist(boolean on) {
        prop.setProperty("movementassist", String.valueOf(on));
    }

    public synchronized String getPlayerName() {
        return prop.getProperty("playername");
    }

    public synchronized void setPlayerName(String name) {
        prop.setProperty("playername", name);
    }

    public synchronized ShelobRule getShelobRule() {
        try {
            ShelobRule r = ShelobRule.valueOf(prop.getProperty("shelobrule"));
            return r;
        } catch (IllegalArgumentException e) {
            prop.setProperty("shelobrule", ShelobRule.SHELOB_DIES.toString());
            return ShelobRule.SHELOB_DIES;
        }
    }

    public synchronized void setShelobRule(ShelobRule r) {
        prop.setProperty("shelobrule", r.toString());
    }

    public synchronized GameType getGameType() {
        try {
            GameType t = GameType.valueOf(prop.getProperty("gametype"));
            return t;
        } catch (IllegalArgumentException e) {
            prop.setProperty("gametype", GameType.CLASSIC.toString());
            return GameType.CLASSIC;
        }
    }

    public synchronized void setGameType(GameType r) {
        prop.setProperty("gametype", r.toString());
    }

    public synchronized SpecialCardRule getSpecialCardRule() {
        try {
            SpecialCardRule r = SpecialCardRule.valueOf(prop.getProperty("specialcardrule"));
            return r;
        } catch (IllegalArgumentException e) {
            prop.setProperty("specialcardrule", SpecialCardRule.CLASSIC.toString());
            return SpecialCardRule.CLASSIC;
        }
    }

    public synchronized void setSpecialCardRule(SpecialCardRule r) {
        prop.setProperty("specialcardrule", r.toString());
    }
}